// libjingle
// Copyright 2004--2005, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//  1. Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//  3. The name of the author may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "monarchmediaengine.h"
#include "RtpSenderReceiver.h"

#include <climits>

#include "talk/base/buffer.h"
#include "talk/base/event.h"
#include "talk/base/logging.h"
#include "talk/base/pathutils.h"
#include "talk/base/stream.h"
#include "talk/session/phone/rtpdump.h"
#include "talk/session/phone/rtputils.h"
#include "talk/base/criticalsection.h"

namespace cricket {

///////////////////////////////////////////////////////////////////////////
// Implementation of MonarchMediaEngine.
///////////////////////////////////////////////////////////////////////////
int MonarchMediaEngine::GetCapabilities() {
  int capabilities = 0;
  if (!voice_input_filename_.empty()) {
    capabilities |= MediaEngine::AUDIO_SEND;
  }
  if (!voice_output_filename_.empty()) {
     capabilities |= MediaEngine::AUDIO_RECV;
  }
  if (!video_input_filename_.empty()) {
    capabilities |= MediaEngine::VIDEO_SEND;
  }
  if (!video_output_filename_.empty()) {
   capabilities |= MediaEngine::VIDEO_RECV;
  }
  return capabilities;
}

VoiceMediaChannel* MonarchMediaEngine::CreateChannel() {
  talk_base::FileStream* input_file_stream = NULL;
  talk_base::FileStream* output_file_stream = NULL;

  if (voice_input_filename_.empty() && voice_output_filename_.empty())
    return NULL;
  if (!voice_input_filename_.empty()) {
    input_file_stream = talk_base::Filesystem::OpenFile(
        talk_base::Pathname(voice_input_filename_), "rb");
    if (!input_file_stream) {
      LOG(LS_ERROR) << "Not able to open the input audio stream file.";
      return NULL;
    }
  }

  if (!voice_output_filename_.empty()) {
    output_file_stream = talk_base::Filesystem::OpenFile(
        talk_base::Pathname(voice_output_filename_), "wb");
    if (!output_file_stream) {
      delete input_file_stream;
      LOG(LS_ERROR) << "Not able to open the output audio stream file.";
      return NULL;
    }
  }

  return new MonarchVoiceChannel();
}

VideoMediaChannel* MonarchMediaEngine::CreateVideoChannel(
    VoiceMediaChannel* voice_ch) {
  talk_base::FileStream* input_file_stream = NULL;
  talk_base::FileStream* output_file_stream = NULL;

  //if (video_input_filename_.empty() && video_output_filename_.empty())
    //  return NULL;
 /*
  if (!video_input_filename_.empty()) {
    input_file_stream = talk_base::Filesystem::OpenFile(
        talk_base::Pathname(video_input_filename_), "rb");
    if (!input_file_stream) {
      LOG(LS_ERROR) << "Not able to open the input video stream file.";
      return NULL;
    }
  }

  if (!video_output_filename_.empty()) {
    output_file_stream = talk_base::Filesystem::OpenFile(
        talk_base::Pathname(video_output_filename_), "wb");
    if (!output_file_stream) {
      delete input_file_stream;
      LOG(LS_ERROR) << "Not able to open the output video stream file.";
      return NULL;
    }
  }
  */

  return new MonarchVideoChannel();
}



///////////////////////////////////////////////////////////////////////////
// Implementation of MonarchVoiceChannel.
///////////////////////////////////////////////////////////////////////////
MonarchVoiceChannel::MonarchVoiceChannel(){
	  //RtpSenderReceiver *r = new RtpSenderReceiver(this);
	  //rtp_sender_receiver_.reset(r);
}

MonarchVoiceChannel::~MonarchVoiceChannel() {}

bool MonarchVoiceChannel::SetSendCodecs(const std::vector<AudioCodec>& codecs) {
  // TODO: Check the format of RTP dump input.
  return true;
}


bool MonarchVoiceChannel::PressDTMF(int i, bool b) {
  return true;
}

bool MonarchVoiceChannel::SetSend(SendFlags flag) {
  //return rtp_sender_receiver_->SetSend(flag != SEND_NOTHING);
  return true;
}

void MonarchVoiceChannel::OnPacketReceived(talk_base::Buffer* packet) {
  //rtp_sender_receiver_->OnPacketReceived(packet);
}

///////////////////////////////////////////////////////////////////////////
// Implementation of MonarchVideoChannel.
///////////////////////////////////////////////////////////////////////////
MonarchVideoChannel::MonarchVideoChannel(){

  RtpSenderReceiver *r = new RtpSenderReceiver(this);
  rtp_sender_receiver_.reset(r);
}


MonarchVideoChannel::~MonarchVideoChannel() {}

bool MonarchVideoChannel::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
  // TODO: Check the format of RTP dump input.
  return true;
}

bool MonarchVideoChannel::SetSend(bool send) {
  return rtp_sender_receiver_->SetSend(send);
}

void MonarchVideoChannel::OnPacketReceived(talk_base::Buffer* packet) {
  rtp_sender_receiver_->OnPacketReceived(packet);
}

}  // namespace cricket




